package com.ddtech.platform;

import cn.hutool.core.codec.Base64;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

import java.io.UnsupportedEncodingException;
import java.util.Map;

public class KettleClient extends AbstractClient {
    private static final String POINT_EXECUTE_TRANS = "/kettle/executeTrans/";

    /**
     * @param serverUrl
     */
    public KettleClient(String serverUrl) {
        super(serverUrl);
    }


    /**
     * 跑kettle 模板
     *
     * @throws UnsupportedEncodingException
     */
    public void executTrans(String templatePath, String user, String password, Map<String, Object> extraParameters) throws Exception {
        String endPoint = getEndPointUrl(POINT_EXECUTE_TRANS);

        String auth = user + ":" + password;
        auth = "Basic " + Base64.encode(auth.getBytes("utf-8"));
        HttpResponse<String> ret = Unirest.post(endPoint)
                .header("Content-Type", "application/x-www-form-urlencoded")
                .header("Authorization", auth)
                .fields(extraParameters)
                .field("level", "Debug")
                .field("trans", templatePath).asString();

        System.out.println(ret.getBody());
    }


    @Override
    protected String getEndPointUrl(String endPoint) {
        return this.baseUrl + endPoint;
    }

    @Override
    protected void doTest() {
        //do nothing
    }
}
